package com.carefree.system.modules.admin.controller;

import com.carefree.common.core.Result;
import com.carefree.common.emus.BusinessTypeEnum;
import com.carefree.common.emus.OperatorTypeEnum;
import com.carefree.common.valid.AddGroup;
import com.carefree.common.valid.UpdateGroup;
import com.carefree.system.common.annotation.SystemLog;
import com.carefree.system.modules.admin.entity.MenuEntity;
import com.carefree.system.modules.admin.service.MenuService;
import com.carefree.system.modules.admin.vo.AssginMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 菜单
 *
 * @author tengbin
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Resource
    private MenuService menuService;


    /**
     * 列表
     *
     * @param params 分页参数
     * @return
     */
    @SystemLog(value = "查询菜单列表并以树形结构显示", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "查询菜单列表并以树形结构显示")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('btn:menu:list')")
    public Result<List<MenuEntity>> list() {
        List<MenuEntity> menuList = menuService.getMenusWithTree();
        return Result.success(menuList);
    }

    /**
     * 信息
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据id查询菜单信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id查询菜单信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('btn:menu:list')")
    public Result<MenuEntity> info(@PathVariable("id") Long id) {
        MenuEntity menu = menuService.getById(id);
        return Result.success(menu);
    }

    /**
     * 保存
     *
     * @param Menu 菜单
     * @return
     */
    @SystemLog(value = "保存菜单信息", businessType = BusinessTypeEnum.INSERT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "保存菜单信息")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('btn:menu:add')")
    public Result<Object> save(@Validated({AddGroup.class}) @RequestBody MenuEntity menu) {
        return menuService.saveMenu(menu) ? Result.ok() : Result.error();
    }

    /**
     * 修改
     *
     * @param Menu 菜单
     * @return
     */
    @SystemLog(value = "修改菜单信息", businessType = BusinessTypeEnum.UPDATE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "修改菜单信息")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('btn:menu:update')")
    public Result<Object> update(@Validated({UpdateGroup.class}) @RequestBody MenuEntity menu) {
        return menuService.updateMenuById(menu) ? Result.ok() : Result.error();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @SystemLog(value = "根据id删除菜单信息", businessType = BusinessTypeEnum.DELETE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id删除菜单信息")
    @DeleteMapping("/delete/{menuId}")
    @PreAuthorize("hasAuthority('btn:menu:remove')")
    public Result<Object> delete(@PathVariable Long menuId) {
        return menuService.removeMenuById(menuId) ? Result.ok() : Result.error();
    }

    /**
     * 启用或禁用(预留)
     *
     * @param id
     * @param status
     * @return
     */
    @SystemLog(value = "根据菜单id启用或禁用菜单", businessType = BusinessTypeEnum.STATUS, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据菜单id启用或禁用菜单")
    @GetMapping("/active/{id}/{status}")
    @PreAuthorize("hasAuthority('btn:menu:enable')")
    public Result<Object> activeOrInActiveById(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return menuService.activeOrInActiveById(id, status) ? Result.ok() : Result.error();
    }

    /**
     * 查询type属性值不是2(按钮)的菜单信息
     *
     * @return
     */
    @SystemLog(value = "查询type属性值不是2(按钮)的菜单信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "查询type属性值不是2(按钮)的菜单信息")
    @GetMapping("/nobuttons")
    @PreAuthorize("hasAuthority('btn:menu:list')")
    public Result<Object> queryNotButtonTypeMenuList() {
        List<MenuEntity> menuEntities = menuService.queryNotButtonTypeMenuList();
        return Result.success(menuEntities);
    }

    /**
     * 根据角色id获取菜单信息
     *
     * @param roleId
     * @return
     */
    @SystemLog(value = "根据角色id获取菜单信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据角色id获取菜单信息")
    @GetMapping("/assign/{roleId}")
    @PreAuthorize("hasAuthority('btn:menu:list')")
    public Result<List<MenuEntity>> toAssignMenusByRoleId(@PathVariable("roleId") Long roleId) {
        List<MenuEntity> menuList = menuService.queryMenusByRoleIdToAssign(roleId);
        return Result.success(menuList);
    }

    /**
     * 根据角色id分配菜单
     *
     * @param assginMenuVo
     * @return
     */
    @SystemLog(value = "根据角色id分配菜单", businessType = BusinessTypeEnum.INSERT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据角色id分配菜单")
    @PostMapping("/assign")
    @PreAuthorize("hasAuthority('btn:menu:assignAuth')")
    public Result<Object> doAssignMenus(@RequestBody AssginMenuVo assginMenuVo) {
        return menuService.assignMenuForRole(assginMenuVo) ? Result.ok() : Result.error();
    }


}
